package com.yeshen.pmtea.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by yuyanghui on 16/9/9.
 * httputils,网络下载数据,bitmap的集合工具类
 */
public class HttpUtils {

    /**
     * 根据 网址请求网络  得到josn数据
     *
     * @param path 网址
     * @return String数据
     */
    public static String getJsonDataFromNet(String path) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();

            connection.setDoInput(true);
            connection.setConnectTimeout(5000);
            connection.connect();


            if (connection.getResponseCode() == 200) {
                InputStream inputStream = connection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int len = 0;
                byte[] b= new byte[1024];
                while ((len=inputStream.read(b))!=-1) {
                    baos.write(b,0,len);
                    baos.flush();
                }
                baos.close();
                inputStream.close();
                //Log.i(TAG, "HttpUtils  getJsonFromNet: "+baos.toString());
                return baos.toString();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
    public static String getNewsJson(String path,String api) {
        try {
            URL url = new URL(path);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write("id="+api);
            bw.flush();
            int code = urlConnection.getResponseCode();
            if (code == 200) {
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                return sb.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

//
//    //获得字节数组
//
//        public static byte[] getByteArrayDataFromNet(String path){
//            try {
//                HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
//
//                connection.setDoInput(true);
//                connection.setConnectTimeout(5000);
//                connection.connect();
//
//                if (connection.getResponseCode() == 200) {
//                    InputStream inputStream = connection.getInputStream();
//                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                    int len = 0;
//                    byte[] b= new byte[1024];
//                    while ((len=inputStream.read(b))!=-1) {
//                        baos.write(b,0,len);
//                        baos.flush();
//                    }
//                    baos.close();
//                    inputStream.close();
//                    //Log.i(TAG, "HttpUtils  getJsonFromNet: "+baos.toString());
//                    return baos.toByteArray();
//                }
//
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//            return null;
//        }

        //获取bitmap
    public static Bitmap getBitmapDataFromNet(String path){
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();

            connection.setDoInput(true);
            connection.setConnectTimeout(5000);
            connection.connect();

            if (connection.getResponseCode() == 200) {
                InputStream inputStream = connection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int len = 0;
                byte[] b= new byte[1024];
                while ((len=inputStream.read(b))!=-1) {
                    baos.write(b,0,len);
                    baos.flush();
                }

                baos.close();
                inputStream.close();
                //Log.i(TAG, "HttpUtils  getJsonFromNet: "+baos.toString());
             return BitmapFactory.decodeByteArray(baos.toByteArray(),0,baos.toByteArray().length);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

}
